package com.egaleye.mqbus.rabbitmq;

import com.egaleye.core.context.EgaleyeApplicationContext;
import com.egaleye.mqbus.core.annotation.Consumer;
import com.egaleye.mqbus.core.consumer.AbstractConsumerInitator;
import com.egaleye.mqbus.core.consumer.ConsumerHandler;
import com.egaleye.mqbus.core.model.ConsumerInfo;
import org.springframework.util.StringUtils;

import java.util.*;

public class RabbitMQConsumerLoader extends AbstractConsumerInitator {

    @Override
    public List<ConsumerInfo> initConsumers() {
        Map<String,Object>  consumerBeanMap =  EgaleyeApplicationContext.getApplicationContext().getBeansWithAnnotation(Consumer.class);
        List<ConsumerInfo> consumerInfos = new ArrayList<>();
        Collection<Object> consumers = consumerBeanMap.values();
        Iterator it = consumers.iterator();
        while ( it.hasNext() ){
            Object consumerHandler = it.next();
            Consumer consumerAnnotation = consumerHandler.getClass().getAnnotation(Consumer.class);
            if( consumerAnnotation != null ) {
                 String topic = consumerAnnotation.topic();
                 if( !StringUtils.hasLength(topic) ) {
                     //topic
                     ConsumerHandler handler = (ConsumerHandler) consumerHandler;
                     consumerAnnotation.msgDataType();
                     consumerInfos.add(new ConsumerInfo());
                 }
            }
        }
        return super.initConsumers();
    }
}
